﻿/**
 * Copyright © TnT-VNGroup 2011
 * PROJECT:   SmartCollector
 * $HeadURL: http://smartcollector.googlecode.com/svn/branches/Srcs/SmartCollector.HtmlParser/DataManager/SiteStructureManager.cs $
 * $Author: chithanh12@gmail.com $
 * $Date: 2011-04-20 18:46:52 +0000 (Wed, 20 Apr 2011) $
 * $Rev: 94 $
 **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartCollector.Domain;
using SmartCollector.Domain.Repository;
using SmartCollector.HtmlParser.Utilities;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace HtmlParser.SiteManager
{
    public class SiteStructureManager
    {
        #region Constant
        public const string FILE_CONFIG = "sitesConfig.xml";
        public const string CLASS = "class";
        public const string ID = "id";
        #endregion

        #region Members
        private static readonly List<SiteItemStructure> m_siteStructureCollections = new List<SiteItemStructure>();
        private static SiteStructureManager m_instance;
        private static object locker = new object();
        #endregion

        /// <summary>
        /// Gets a value indicating whether this instance is dirty.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is dirty; otherwise, <c>false</c>.
        /// </value>
        public bool IsDirty
        {
            get
            {
                return m_siteStructureCollections.Where(p => p.IsDirty).FirstOrDefault() != null;
            }
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static SiteStructureManager Instance
        {
            get
            {
                if(m_instance == null)
                {
                    lock (locker)
                    {
                        if (m_instance == null)
                        {
                            m_instance = new SiteStructureManager();
                        }
                    }
                }
                return m_instance;
            }
        }

        #region Constructor
        /// <summary>
        /// Initializes the <see cref="SiteStructureManager"/> class.
        /// </summary>
        public SiteStructureManager()
        {
            LoadConfig();
        }
        #endregion

        /// <summary>
        /// Gets the site item structure.
        /// </summary>
        /// <param name="siteId">The site id.</param>
        /// <returns></returns>
        public SiteItemStructure GetSiteItemStructure(int siteId)
        {
            return m_siteStructureCollections.Where(p => p.SiteId == siteId).FirstOrDefault();
        }

        public SiteItemStructure GetSiteItemStructure(string siteName)
        {
            return m_siteStructureCollections.Where(p => p.SiteDomainName.ToLower() == siteName.ToLower()).FirstOrDefault();
        }
        /// <summary>
        /// Registers the new site structure.
        /// </summary>
        /// <param name="siteId">The site id.</param>
        /// <param name="structute">The structute.</param>
        public void RegisterNewSiteStructure(SiteItemStructure structute)
        {
            if (!m_siteStructureCollections.Contains(structute))
            {
                m_siteStructureCollections.Add(structute);
            }
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        private void LoadConfig()
        {
            #region Comments
            //TODO: Implement the LoadConfig from XML
            // I just create a simple SiteStructure for vnExpress for Test
            //SiteItemStructure vnExpress = new SiteItemStructure();
            //vnExpress.SiteId = 1;
            //vnExpress.SiteDomainName = "http://vnexpress.net/";

            //TagSelector selector = new TagSelector();

            ////Boundary
            //selector.AddConstraint(CLASS, "content");
            //vnExpress.AddConstraint(RegionArea.Boundary, selector);

            ////Title
            //selector = new TagSelector();
            //selector.AddConstraint(CLASS, "title");
            //vnExpress.AddConstraint(RegionArea.Title, selector);

            //// Description
            //selector = new TagSelector();
            //selector.AddConstraint(CLASS, "lead");
            //vnExpress.AddConstraint(RegionArea.Description, selector);

            ////Content
            ////selector = new TagSelector();
            ////selector.AddConstraint(CLASS, "title");
            ////vnExpress.AddConstraint(RegionArea.Content, selector);

            ////Author
            //selector = new TagSelector();
            //selector.AddConstraint(CLASS, "normal");
            //selector.AddConstraint("align", "right");
            //vnExpress.AddConstraint(RegionArea.Author, selector);

            ////Source
            ////selector = new TagSelector();
            ////selector.AddConstraint(CLASS, "title");
            ////vnExpress.AddConstraint(RegionArea.Title, selector);

            ////Nearest Div
            //selector = new TagSelector();
            //selector.AddConstraint(CLASS, "tag-parent");
            //vnExpress.AddConstraint(RegionArea.NearestBoundary, selector);
            //m_siteStructureCollections.Add(vnExpress);
            #endregion
            m_siteStructureCollections.AddRange(
                SerializeHelper.DeserializeStructure(string.Format("{0}/Configs/{1}",Application.StartupPath,FILE_CONFIG))
                );
            for (int i = 0; i < m_siteStructureCollections.Count; i++)
            {
                m_siteStructureCollections[i].IsDirty = false;
            }
        }

        /// <summary>
        /// Saves to XML.
        /// </summary>
        public void SaveToXml()
        {
            SerializeHelper.SerializeSiteStructure(m_siteStructureCollections, string.Format("{0}/Configs/{1}", Application.StartupPath, FILE_CONFIG));
            for (int i = 0; i < m_siteStructureCollections.Count; i++)
            {
                m_siteStructureCollections[i].IsDirty = false;
            }
        }
    }
}
